package com.turing.usdtpay.mvp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseActivity;
import com.turing.usdtpay.mvp.contract.NickNameContract;
import com.turing.usdtpay.mvp.presenter.NickNamePresent;
import com.turing.usdtpay.mvp.view.BaseTitleView;
import com.turing.usdtpay.utils.ToastUtils;

/**
 * @作者：JAY
 * @创建时间：2019/10/16 17:55
 * @描述：
 **/
public class NickNameActivity extends Activity implements View.OnClickListener , NickNameContract.View {

    private BaseTitleView mTitle;
    private Button mCommit;
    private EditText mName;
    private NickNameContract.Present mPresent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nick_name);
        initView();
        initTitle();
        setPresenter(new NickNamePresent(this));
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null){
            String name = intent.getStringExtra("name");
            if (!TextUtils.isEmpty(name)){
                mName.setText(name);
                mName.setEnabled(false);
            }
        }

    }

    private void initTitle() {
        mTitle.setLeftShow(true);
        mTitle.setLeftButtonListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitle.setTitle("昵称设置");
    }

    private void initView() {
        mTitle = (BaseTitleView) findViewById(R.id.title);
        mCommit = (Button) findViewById(R.id.btn_commit);
        mName = (EditText) findViewById(R.id.et_name);
        mCommit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (TextUtils.isEmpty(mName.getText().toString())){
            ToastUtils.showNormolToast(this,"请输入昵称");
            return;
        }
        mPresent.changeName(this,mName.getText().toString());
    }

    @Override
    public void changeSuccess() {
        ToastUtils.showNormolToast(this,"修改成功");
        Intent intent = new Intent();
        intent.putExtra("name",mName.getText().toString());
        setResult(3,intent);
        finish();
    }

    @Override
    public void getSuccess(String name) {
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showNormolToast(this,msg);
    }

    @Override
    public void setPresenter(NickNameContract.Present presenter) {
        mPresent = presenter;
    }
}
